﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikebuluo.web.admin.actives
{//刮刮卡
    public partial class gglset : weikeweixin.web.admin.cs.adminbase
    {
        h_ggk_dal ggkdal = new h_ggk_dal();
        h_ggk_model ggkmodel = new h_ggk_model();

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.Form["action"])
            {
                case "save"://提交信息
                    save();
                    break;
            }

            switch (Request.QueryString["action"])
            {
                case "bind":
                    bind();
                    break;
                case "del":
                    del();
                    break;
              
            }
        }

        //保存设置
        private void save() {
            DataTable dtggk = ggkdal.GetList("user_id ='" + user_id + "' and ggks_id="+Request.Form["id"]).Tables[0];
            //修改
            for (int i = 0; i < dtggk.Rows.Count; i++)
            {
                int ggk_id = Convert.ToInt32(dtggk.Rows[i]["ggk_id"]);
                ggkmodel = ggkdal.GetModel(ggk_id);
                ggkmodel.ggk_award = Request.Form["cname[" + ggk_id + "]"].ToString();
                ggkmodel.ggk_num = Convert.ToInt32(Request.Form["bscore[" + ggk_id + "]"].Trim());
                ggkmodel.ggk_chance = Convert.ToInt32(Request.Form["escore[" + ggk_id + "]"].Trim());
                ggkmodel.user_id = int.Parse(user_id);
                ggkmodel.ggks_id = int.Parse(Request.Form["id"].Trim().ToString());
                ggkdal.Update(ggkmodel);
            }

            //新增
            for (int j = 1; !string.IsNullOrEmpty(Request.Form["add[cname][" + j + "]"]); j++)
            {
                ggkmodel.ggk_award = Request.Form["add[cname][" + j + "]"].Trim();
                ggkmodel.ggk_num = Convert.ToInt32(Request.Form["add[bscore][" + j + "]"].Trim());
                ggkmodel.ggk_chance = Convert.ToInt32(Request.Form["add[escore][" + j + "]"].Trim());
                ggkmodel.user_id = int.Parse(user_id);
                ggkmodel.ggks_id = int.Parse(Request.Form["id"].Trim().ToString());
                ggkdal.Add(ggkmodel);
            }
            Response.Write("{\"errno\":\"0\",\"tip\":\"设置成功！\",\"url\":\"ggklist.aspx\",\"error\":\"\"}");
            Response.End();
        }

        //删除
        private void del() {
            bool bl = ggkdal.Delete(int.Parse(Request.QueryString["ggk_id"].Trim()));
        }
        //绑定
        private void bind() {
            DataTable dtggk = ggkdal.GetList("user_id ='" + user_id + "' and ggks_id="+Request.QueryString["id"]).Tables[0];
            JsonOperate JsonOperate = new JsonOperate();
            Response.Write(JsonOperate.TableToBasicJson(dtggk));
            Response.End();
        }

    }
}